package com.example.demo.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.Mapper.ReadFeedbackMapper;
import com.example.demo.base.ArrayCountUtils;
import com.example.demo.base.Result;
import com.example.demo.entity.Commodity;
import com.example.demo.entity.ReadFeedback;
import com.example.demo.entity.unReadFeedback;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/readFeedback")
public class ReadFeedbackController {
    @Resource
    ReadFeedbackMapper readFeedbackMapper;

    /**
     * 获取数据库全部数据  √
     *
     * @return
     */
    @GetMapping("/all")
    public Result<?> findAll(@RequestParam String tenantId) {
        QueryWrapper<ReadFeedback> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("tenant_id", Integer.valueOf(tenantId));
        return Result.success(readFeedbackMapper.selectList(queryWrapper));
    }

    /**
     * 生成投诉数组  √
     * @return
     */

    @GetMapping("/chatData")
    public Result<?> chatData(@RequestParam String tenantId) {
        QueryWrapper<ReadFeedback> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("tenant_id", Integer.valueOf(tenantId));
        List<ReadFeedback> ReadFeedbackInfoList = readFeedbackMapper.selectList(queryWrapper);
        String[] res = new String[ReadFeedbackInfoList.size()];
        for(int i = 0; i < ReadFeedbackInfoList.size(); i++)
        {
            res[i] = ReadFeedbackInfoList.get(i).getProductName();
        }
        ArrayCountUtils arrayCountUtils = new ArrayCountUtils();
        List<ArrayCountUtils.Group> groups = arrayCountUtils.find(res);
        return Result.success(groups);
    }
    /**
     * 分页查询未读反馈信息中所有数据  √
     *
     * @param pageNum
     * @param pageSize
     * @return
     */

    @GetMapping(value = "/pageAll")
    public Result<?> findPageU(@RequestParam(defaultValue = "1") Integer pageNum,
                               @RequestParam(defaultValue = "10") Integer pageSize,
                               @RequestParam String tenantId) {
        LambdaQueryWrapper<ReadFeedback> wrapper = Wrappers.<ReadFeedback>lambdaQuery();
        wrapper.eq(ReadFeedback::getTenantId, Integer.valueOf(tenantId));
        Page<ReadFeedback> ReadFeedbackPage = readFeedbackMapper.selectPage(new Page<>(pageNum, pageSize), wrapper);
        return Result.success(ReadFeedbackPage);
    }

    @GetMapping("/findOne")
    public Result<?> findOne(@RequestParam String identify,
                             @RequestParam String name) {
        LambdaQueryWrapper<ReadFeedback> wrapper = Wrappers.<ReadFeedback>lambdaQuery();
        wrapper.eq(ReadFeedback::getIdentify, identify).eq(ReadFeedback::getName, name);
        ReadFeedback Read = readFeedbackMapper.selectOne(wrapper);
        if (Read != null) {
            return Result.success(Read);
        }
        return Result.error(0, "没有查询到相关数据");
    }

    /**
     * 新增
     *
     * @param name
     * @param sex
     * @param identify
     * @param tel
     * @param email
     * @param address
     * @param productCode
     * @param productName
     * @param feedbackInfo
     * @return
     */
    @GetMapping(value = "/add")
    public Result<?> saveU(@RequestParam String name,
                           @RequestParam String sex,
                           @RequestParam String identify,
                           @RequestParam String tel,
                           @RequestParam String email,
                           @RequestParam String address,
                           @RequestParam String productCode,
                           @RequestParam String productName,
                           @RequestParam String feedbackInfo,
                           @RequestParam String tenantId) {
        if (name.isEmpty() || sex.isEmpty() || identify.isEmpty() || tel.isEmpty() || email.isEmpty() || address.isEmpty() || productCode.isEmpty() || productName.isEmpty()) {
            return Result.error(0, "文本不能为空");
        }
        ReadFeedback Read = new ReadFeedback();
        Read.setName(name);
        Read.setSex(sex);
        Read.setIdentify(identify);
        Read.setTel(tel);
        Read.setEmail(email);
        Read.setAddress(address);
        Read.setProductCode(productCode);
        Read.setProductName(productName);
        Read.setFeedbackInfo(feedbackInfo);
        Read.setTenantId(Integer.valueOf(tenantId));
        readFeedbackMapper.insert(Read);
        return Result.success();
    }
    /**
     * 根据identify更新   √
     *
     * @param name
     * @param sex
     * @param identify
     * @param tel
     * @param email
     * @param address
     * @param productCode
     * @param productName
     * @param feedbackInfo
     * @return
     */

    @GetMapping(value = "/update")
    public Result<?> updateU(@RequestParam String name,
                             @RequestParam String sex,
                             @RequestParam String identify,
                             @RequestParam String tel,
                             @RequestParam String email,
                             @RequestParam String address,
                             @RequestParam String productCode,
                             @RequestParam String productName,
                             @RequestParam String feedbackInfo,
                             @RequestParam String tenantId) {
        if (name.isEmpty() || sex.isEmpty() || identify.isEmpty() || tel.isEmpty() || email.isEmpty() || address.isEmpty() || productCode.isEmpty() || productName.isEmpty()) {
            return Result.error(0, "文本不能为空");
        }
        ReadFeedback Read = new ReadFeedback();
        Read.setName(name);
        Read.setSex(sex);
        Read.setIdentify(identify);
        Read.setTel(tel);
        Read.setEmail(email);
        Read.setAddress(address);
        Read.setProductCode(productCode);
        Read.setProductName(productName);
        Read.setFeedbackInfo(feedbackInfo);
        Read.setTenantId(Integer.valueOf(tenantId));
        readFeedbackMapper.updateByMultiId(Read);
        return Result.success();
    }

    /**
     * 根据身份证查询信息  √
     *
     * @param identify
     * @return
     */
    @GetMapping("/findById")
    public Result<?> findByFarmId(@RequestParam String identify,
                                  @RequestParam String tenantId) {

        ReadFeedback read = new ReadFeedback();
        read.setIdentify(identify);
        read.setTenantId(Integer.valueOf(tenantId));
        ReadFeedback tmp = readFeedbackMapper.selectByMultiId(read);
        List<ReadFeedback> ReadFeedbackInfoList = new ArrayList<>();
        if (tmp != null) {
            ReadFeedbackInfoList.add(tmp);
            return Result.success(ReadFeedbackInfoList);
        }
        return Result.error(0, "没有查询到相关数据");
    }

    /**
     * 根据name模糊查询  √
     *
     * @param name
     * @return
     */
    @GetMapping("/findByName")
    public Result<?> findByName(@RequestParam(defaultValue = "1") Integer pageNum,
                                @RequestParam(defaultValue = "10") Integer pageSize,
                                @RequestParam String name,
                                @RequestParam String tenantId) {
        LambdaQueryWrapper<ReadFeedback> wrapper = Wrappers.<ReadFeedback>lambdaQuery();
        wrapper.like(ReadFeedback::getName, name)
                .eq(ReadFeedback::getTenantId,tenantId);
        Page<ReadFeedback> ReadFeedbackPage = readFeedbackMapper.selectPage(new Page<>(pageNum, pageSize), wrapper);
        if (ReadFeedbackPage.getTotal() == 0) {
            return Result.error(0, "没有查询到相关数据");
        }
        return Result.success(ReadFeedbackPage);
    }

    /**
     * 根据身份证删除信息
     * @param identify
     * @return
     */

    @DeleteMapping("/delById")
    public Result<?> delete(@RequestParam String identify,
                            @RequestParam String tenantId) {
        ReadFeedback read = new ReadFeedback();
        read.setIdentify(identify);
        read.setTenantId(Integer.valueOf(tenantId));
        int res = readFeedbackMapper.deleteByMultiId(read);
        if (res > 0)
            return Result.success();
        return Result.error(0, "删除失败");
    }
}
